<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>消息-云校通</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
</head>
<body ontouchstart id="xs" style="background: #fff;">
	<div class="weui_tab tab-bottom">
		<div class="weui_tab_bd">
			<div class="weui_panel weui_panel_access" id="messageList">
            	<c:forEach var="item" items="${notices }">
            		<a href="<%=basePath %>/xs/notices/list?sid=${user.parent.studentId}&nt=${item.noticeType}" style="">
	                    <div class="weui_cell slidelefts p10">
		                    <div class="weui_media_hd">
		                        <img class="weui_left_img" src="<%=basePath %>/images/notice_${item.noticeType }.png" alt="">
		                    </div>
		                    <div class="weui_cell_bd weui_cell_primary weui_media_box borderNoneBefore">
		                        <h4 class="weui_media_title">
		                        	<c:choose>
		                        		<c:when test="${item.noticeType == 1 }">班级通知</c:when>
		                        		<c:when test="${item.noticeType == 2 }">科级通知</c:when>
		                        		<c:when test="${item.noticeType == 3 }">文件传阅</c:when>
		                        		<c:when test="${item.noticeType == 4 }">学校通知</c:when>
		                        		<c:when test="${item.noticeType == 5 }">作业通知</c:when>
		                        	</c:choose>	
		                        </h4>
		                        <p class="xs-time"><fmt:formatDate value="${item.createTime}" type="time" timeStyle="short"/></p>
		                        <p class="weui_media_desc f13">${item.content }</p>
		                        <c:if test="${item.count > 0 }"><span class="xs-num">${item.count }</span></c:if>
		                    </div>
		                </div>
	                </a>
            	</c:forEach>
                
            </div>
        </div>
	</div>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script>
	$(function() {
		var userRole = "${user.roleType}";
		var reqData = getReqChatList(userRole);
		$.ajax({
          	url : basePath + "/message/chat",
  	 		type : "post",
  	 		data : JSON.stringify(reqData),
		    contentType: "application/json",
		    dataType : 'json',
		    async:false,
            success: function(data){
            	var chatData = getChatList(userRole);
            	var html = "";
            	if(data != null && data.length > 0) {
            		for(var i in data) {
            			html += loadHtml(data[i])
                        for(var j in chatData) {
                        	if(data[i].chatType == chatData[j].chatType) {
                        		if(data[i].chatType == 'group' && (data[i].schoolId = chatData[j].schoolId 
                                && data[i].grade == chatData[j].grade && data[i].clazz == chatData[j].clazz)) {
                        			chatData.splice(chatData.indexOf(chatData[j]),1);
                        			break;
                        		}else if(data[i].chatType == 'private' && (data[i].schoolId = chatData[j].schoolId 
                              		  && data[i].grade == chatData[j].grade && data[i].clazz == chatData[j].clazz
                              		  && data[i].studentId == chatData[j].studentId)) {
                        			chatData.splice(chatData.indexOf(chatData[j]),1);
                        			break;
                        		}
                        	}
                        }
            		}
            	}
        		for(var j in chatData) {
        			html += loadHtml1(chatData[j])
        		}
        		$("#messageList").append(html);
            },
            error: function(xhr, type){
              
            }
          });
	});
	function loadHtml(item) {
		if(!item.message){
			item.message = "";
		}
		var html = "";
		if(item.chatType == 'group' && !(item.grade == parseInt("${user.parent.grade}") && item.clazz == parseInt("${user.parent.clazz}"))) {
			return '';
		}
		html += '<a href="<%=basePath%>'+ (item.chatType == 'group' ? '/xs/groupChat/class?sid=${user.parent.studentId}&grade='+ item.grade +'&clazz='+ item.clazz : '/xs/groupChat/class/sx?sid=${user.parent.studentId}&grade='+ item.grade +'&clazz='+ item.clazz + '&tsid=' + item.studentId) +'" class="weui_media_box weui_media_appmsg">';
		html += '<div class="weui_media_hd">';
        html += '<img class="weui_media_appmsg_thumb" src="<%=basePath%>/images/siliao_jiazhang@3x.png" alt="">';
        html += '</div>';
        html += '<div class="weui_media_bd">';
        html += '<h4 class="weui_media_title">';
        if(item.chatType == 'group') html += item.gradeName+'（'+item.clazz+'）班 留言板';
        else if (item.chatType == 'private') html += item.studentName + ' 私信留言';
        html += '</h4>';
        html += '<p class="xs-time">'+ getDateDiff(item.createTime, 'yyyy/MM/dd')+'</p>';
        html += '<p class="weui_media_desc">'+ (item.messageType=='image' ? '[图片]' : item.message) +'</p>';
        html += '<span class="xs-num">'+(item.count > 99 ? '99+' : item.count)+'</span>';
        html += '</div></a>';
        return html;
	}
	function loadHtml1(item) {
		if(!item.message) return '';
		var html = "";
		if(item.chatType == 'group' && !(item.grade == parseInt("${user.parent.grade}") && item.clazz == parseInt("${user.parent.clazz}"))) {
			return '';
		}
		html += '<a href="<%=basePath%>'+ (item.chatType == 'group' ? '/xs/groupChat/class?sid=${user.parent.studentId}&grade='+ item.grade +'&clazz='+ item.clazz : '/xs/groupChat/class/sx?sid=${user.parent.studentId}&grade='+ item.grade +'&clazz='+ item.clazz + '&tsid=' + item.studentId) +'" class="weui_media_box weui_media_appmsg">';
		html += '<div class="weui_media_hd">';
        html += '<img class="weui_media_appmsg_thumb" src="<%=basePath%>/images/siliao_jiazhang@3x.png" alt="">';
        html += '</div>';
        html += '<div class="weui_media_bd">';
        html += '<h4 class="weui_media_title">';
        if(item.chatType == 'group') html += getGradeName("${term.termKey}", item.grade)+'（'+item.clazz+'）班 留言板';
        else if (item.chatType == 'private') html += item.studentName + ' 私信留言';
        html += '</h4>';
        html += '<p class="xs-time">'+getDateDiff(item.messageDate, 'yyyy/MM/dd')+'</p>';
        html += '<p class="weui_media_desc">'+ item.message +'</p>';
        html += '</div></a>';
        return html;
	}
</script>
</body>
</html>